﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class ExaminationEdit : PageBase
    {
        #region ViewPower
        private string ID
        {
            get
            {
                return GetQueryIntValue("ID");
            }
        }
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            var example = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (example != null)
            {
                var app = ProcesUtil.GetApplycation(example.AppllicationID);
                this.hdAppID.Value = app.AppllicationID;
                this.hdAppLoginID.Value = app.LoginID;
                this.hdCurrentStandID.Value = this.CurrentStandID;
                this.hdUserName.Value = app.UserName;
            }
            if (ReadOnly == "1")
            {
                if (!IsAdmin)
                    this.btnSaveClose.Hidden = true;
            }
        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            var oModel = DB.partyjoinexaminations.Find(this.ID);
            if (oModel == null)
            {
                this.txtTitle.Text = "关于" + this.hdUserName.Value + "同志之____的证明材料";
            }
            else
            {
                this.txtTitle.Text = oModel.Title;
                this.heContent.Text = oModel.Content;
            }
        }



        private void SaveData()
        {
            bool bResult = false;
            var oModel = DB.partyjoinexaminations.Find(this.ID);
            if (oModel == null)
            {
                oModel = new ZHDJ.Data.Models.JoinParty.partyjoinexamination();
                oModel.ID = StringHelper.NewID;
                oModel.StandID = CurrentStandID;
                oModel.AppllicationID = this.AppllicationID;
                oModel.CreateBy = CurrentUser.LoginID;
                oModel.CreateTime = DateTime.Now;
                oModel.Title = this.txtTitle.Text.Replace("_", "");
                oModel.Content = this.heContent.Text;
                DB.partyjoinexaminations.Add(oModel);
                bResult = DB.SaveChanges() == 1;
            }
            else
            {
                oModel.StandID = CurrentStandID;
                oModel.AppllicationID = this.AppllicationID;
                bResult = DB.SaveChanges() == 1;
            }

            if (bResult)
            {
                Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
            }
        }


        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.SaveData();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}